package com.zc.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zc.dao.impl.ArticleDaoImpl;
import com.zc.model.Article;
import com.zc.model.User;

public class ArticleUpdateAction extends ActionSupport implements ModelDriven<Article> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Article article = new Article();

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	@Override
	public String execute() throws Exception {
		ActionContext context = ActionContext.getContext();

		Map<String, Object> session = context.getSession();
		System.out.println("输出articl的内容------------------" + article.toString());
		ArticleDaoImpl articleDao = new ArticleDaoImpl();
		// article.setCreator("创建者");
		// article.setCrateTime(new Date().toString());
		article.setLastestEditor(((User) session.get("user")).getUsername());
		article.setLastestEditTime(new Date().toString());
		if (articleDao.updateArticle(article)) {
			List<Article> articleList = articleDao.getAllArticle();
			session.put("articles", articleList);
			System.out.println(article.toString());
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	@Override
	public Article getModel() {
		// TODO Auto-generated method stub
		System.out.println(article.toString());
		return this.article;
	}

}
